package com.lu.adog.demo.main.page.view.sortpreview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lu.adog.R;
import com.lu.adog.demo.common.CommonActivity;
import com.lu.adog.util.Logg;
import com.lu.adog.view.SortPreView;

import java.util.Random;

/**
 * @author Yue
 * @date 2017/11/30 11:13
 */

public class SortPreViewActivity extends CommonActivity {
    private SortPreView spvSortStatistics;
    private int[] sortData = {13, 34, 67, 8, 34, 32, 29};
    private Button btnStartSort;
    private Button btnInitData;
    private TextView tvInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sortpreview);
        assignViews();
    }

    private void assignViews() {
        spvSortStatistics = (SortPreView) findViewById(R.id.spv_sortStatistics);
        btnStartSort = (Button) findViewById(R.id.btn_startSort);
        btnInitData = (Button) findViewById(R.id.btn_initData);
        tvInfo = (TextView) findViewById(R.id.tv_info);

        spvSortStatistics.setNums(sortData);

        btnStartSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sortGo();
            }
        });

        btnInitData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isSortCompelete){
                    Random random = new Random();
                    int count = 5 + random.nextInt(10);
                    sortData = new int[count];
                    int max = (int) spvSortStatistics.getLimitNum();
                    for (int i = 0;i<count;i++){
                        sortData[i] = random.nextInt(max);
                    }
                    spvSortStatistics.setNums(sortData);
                    spvSortStatistics.update();
                }
            }
        });

    }
    private boolean isSortCompelete = true;
    private void sortGo() {
        if (!isSortCompelete){
            return;
        }


        new Thread(new Runnable() {
            private int sortCount;
            @Override
            public void run() {
                isSortCompelete = false;
                for (int i = 0; i < sortData.length - 1; i++) {//外层循环控制排序趟数
                    for (int j = 0; j < sortData.length - 1 - i; j++) {//内层循环控制每一趟排序多少次
                        if (sortData[j] > sortData[j + 1]) {
                            int temp = sortData[j];
                            sortData[j] = sortData[j + 1];
                            sortData[j + 1] = temp;
                            sortCount++;

                            try {
                                Thread.sleep(1000);
                                Logg.w(">>>",sortCount+"");
                                spvSortStatistics.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        spvSortStatistics.update();
                                        tvInfo.setText(sortCount+"");
                                    }
                                });

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                isSortCompelete = true;
            }
        }).start();


    }
}
